const Permission = require('../../mongooses/rbac/Permission')

class PermissionController {
    constructor () {

    }
    // GET 索引列表
    async index (ctx, next) {
        let pageNo = ctx.query.pageNo ? parseInt(ctx.query.pageNo): 1
        let pageSize = ctx.query.pageSize ? parseInt(ctx.query.pageSize): 10
        let query = {status: { $ne: 2}}
        let count = await Permission.find(query).count()
        let data = await Permission.find(query).sort({'_id': -1}).skip(pageSize * (pageNo -1)).limit(pageSize);
        ctx.body = {
            result: data,
            totalCount: count,
            pageSize: pageSize,
            pageNo: pageNo
        }
    }
    // GET 创建显示表单
    async create (ctx, next) {
        ctx.body = 'create';
    }
    //POST 保存创建的数据
    async store (ctx, next) {
        let data = ctx.request.body
        let res = await Permission.create(data)
        
        ctx.body = {
            ret: 0,
            msg: 'OK',
            data: res
        }
    }
    //GET 显示对应id的内容
    async show (ctx, next) {
        let id = ctx.request.body.id

        let data = await Permission.findById(id)

        ctx.body = {
            ret: 0,
            msg: 'OK',
            data: data
        } 
    }
    //GET 编辑（显示表单）
    async edit (ctx, next) {
        ctx.body = 'index';
    }
    // PUT/PATCH保存
    async save (ctx, next) {
        let id = ctx.request.body.id
        let data = ctx.body.request
        let client = await Client.findByIdAndUpdate({ '_id': id }, { $set: data })
        ctx.body = {
            ret: 0,
            msg: 'OK',
            data: res
        }  
    }
    // GET 删除
    async destroy (ctx, next) {
        let id = ctx.params.id
        let res = await Permission.findByIdAndRemove({'_id': id})
        ctx.body = {
            ret: 0,
            msg: 'OK',
            data: res
        } 
    }
}
module.exports = new PermissionController()